package cn.usts.edu.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Objects;
import java.util.Random;

public class GamePanel extends JPanel implements ActionListener  {
/**
 * 游戏面板
 * 游戏中的面板都是通过画笔来画出来
 * */

    // 定义蛇的数据结构
    int length;
    int[] snakeX = new int[50];// 蛇的坐标25*25   < 850/25[34]  * 600/25[24]
    int[] snakeY = new int[50];
    String towards;  // 蛇头方向

    // 食物的位置
    int foodX;
    int foodY;
    int score;  // 分数
    Random random = new Random();

    Boolean isFail;   // 是否撞到自己了
    Boolean isStart;  // 游戏是否开始
    Timer timer = new Timer(200,this);//监听器用在这里了

    public void init(){
        length = 3; // 蛇的初始长度
        snakeX[0] = 100; snakeY[0] = 100;
        snakeX[1] = 75; snakeY[1] = 100;
        snakeX[2] = 50; snakeY[2] = 100;
        towards = "right";
        isStart = false;
        isFail = false;
        foodX = 25+25*random.nextInt(33); // 食物位置随机
        foodY = 75+25*random.nextInt(23);
        score = 0;
    }

    public GamePanel(){
        init();
        // 获得焦点和键盘事件监听
        this.setFocusable(true);
        this.addKeyListener(new GameKeyListener());
        timer.start();

    }


    // 画组件
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);// 清屏,不用会闪烁
        this.setBackground(Color.WHITE);

        // 绘制静态面板
        // 广告栏head
        System.out.println(Data.headURL);
        Data.head.paintIcon(this,g,25,11); // 放到哪个组件上,用哪个画笔,位置
        g.fillRect(25,75,850,600); // 黑色框部分

        // 先画食物后画身体身体会盖住食物,否则食物会浮在上面
        Data.food.paintIcon(this,g,foodX,foodY);

        // 默认头向右
        if (Objects.equals(towards, "right")){
            Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        else if (Objects.equals(towards, "left")){
            Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        else if (Objects.equals(towards, "down")){
            Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
        }
        else if (Objects.equals(towards, "up")){
            Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
        }

        // 把小蛇身体画上去
        for (int i=1;i<length;i++){
            Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
        }

        // 设置按下空格开始/暂停游戏
        if (!isStart){
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));// 粗体,40号
            g.drawString("按下空格开始/暂停游戏",250,300);
        }
        if (isFail){
            g.setColor(Color.RED);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));// 粗体,40号
            g.drawString("游戏失败按下空格重新开始",150,300);
        }

        // 分数
        g.setColor(Color.red);
        g.setFont(new Font("微软雅黑", Font.BOLD, 20));
        g.drawString("分数:",750,40);
        g.drawString(String.valueOf(score),810,40);




    }


    // 监听
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart && !isFail){

            for (int i=length; i > 0;i--){
                snakeX[i] = snakeX[i-1];
                snakeY[i] = snakeY[i-1];
            }

            // 吃食物
            if (snakeX[0]==foodX && snakeY[0]==foodY){
                length++;//身体+1
                score++;
                foodX = 25+25*random.nextInt(33); // 食物位置刷新
                foodY = 75+25*random.nextInt(23);
            }

            // 蛇的移动方向
            if (Objects.equals(towards,"right")){
                snakeX[0] = snakeX[0]+25;// 右移一格
                // 边界判断
                if (snakeX[0]>850){
                    snakeX[0]=25;
                }
            }else if (Objects.equals(towards,"left")){
                snakeX[0] = snakeX[0]-25;
                // 边界判断
                if (snakeX[0]<25){
                    snakeX[0]=850;
                }
            }else if (Objects.equals(towards,"down")){
                snakeY[0] =snakeY[0]+25;
                // 边界判断
                if (snakeY[0]>650){
                    snakeY[0]=75;
                }
            }else if (Objects.equals(towards,"up")){
                snakeY[0] =snakeY[0]-25;
                // 边界判断
                if (snakeY[0]<75){
                    snakeY[0]=650;
                }
            }
            // 失败判断
            for (int i=length;i>0;i--){
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
                    isFail = true;
                    break;
                }
            }
            repaint();
        }


    }

    /*为什么这里使用适配器就无法实现,使用接口可以偶尔实现,直接使用GamePanel实现接口就可以实现 frame.setVisible(true);  */
    // 键盘监听
    class GameKeyListener extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();

            // space监听
            if (keyCode == KeyEvent.VK_SPACE){
                if (isFail){
                    isFail=false;
                    init();
                }else{
                    isStart=!isStart;
                }
                repaint();
            }
            // up监听  往上走的时候不可以按down键 !Objects.equals(towards, "down")
            else if (keyCode == KeyEvent.VK_UP && !Objects.equals(towards, "down")){
                towards = "up";
            }
            // down监听
            else if (keyCode == KeyEvent.VK_DOWN && !Objects.equals(towards, "up")){
                towards = "down";
            }
            // left监听
            else if (keyCode == KeyEvent.VK_LEFT && !Objects.equals(towards, "right")){
                towards = "left";
            }
            // right监听
            else if (keyCode == KeyEvent.VK_RIGHT && !Objects.equals(towards, "left")){
                towards = "right";
            }

        }
    }



}

